package com.greensecurity.sim.controller;

import com.greensecurity.sim.entity.UserMessage;
import com.greensecurity.sim.repository.UserMessageRepository;
import com.greensecurity.sim.service.UserMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/userMessage")
@Api(description = "用户消息管理")
public class UserMessageController {

    @Resource
    private UserMessageRepository userMessageRepository;

    @Resource
    private UserMessageService userMessageService;

    @ApiOperation(value = "获取用户消息")
    @ResponseBody
    @GetMapping("/getUserMessage/{userId}")
    public List<UserMessage> getUserMessage(@PathVariable Long userId){
        return userMessageService.findUserMessageByUserId(userId);
    }

    @ApiOperation(value = "获取是否已读的用户消息")
    @ResponseBody
    @GetMapping("/getIsReadUserMessage/{userId}/{isRead}")
    public List<UserMessage> getIsReadUserMessage(@PathVariable Long userId, @PathVariable Boolean isRead){
        return userMessageService.findUserMessageByUserIdAndIsRead(userId,isRead);
    }

    @ApiOperation(value = "增加用户消息")
    @ResponseBody
    @PostMapping("/save")
    public String saveUserMessage(UserMessage userMessage){
        Date Now = new Date();
        userMessage.setIsRead(false);
        userMessage.setCreateTime(Now);
        userMessage.setUpdateTime(Now);
        userMessageRepository.save(userMessage);
        return "OK";
    }
}
